package cn.well.cloud.core.auth.deniedhandler;

import cn.stylefeng.roses.core.util.RenderUtil;
import cn.stylefeng.roses.kernel.model.response.ErrorResponseData;
import cn.well.cloud.base.auth.exception.enums.AuthExceptionEnum;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtAuthenticationAccessDeniedHandler implements AccessDeniedHandler {
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        // 无权限
        ErrorResponseData errorResponseData = new ErrorResponseData(
                AuthExceptionEnum.NO_PERMISSION.getCode(), AuthExceptionEnum.NO_PERMISSION.getMessage());
        RenderUtil.renderJson(response,errorResponseData);
    }

}
